package org.zju.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.zju.demo.model.User;
import org.zju.demo.model.request.UserJson;
import org.zju.demo.model.response.BasicResponseJson;
import org.zju.demo.model.response.UserDetailJson;
import org.zju.demo.service.intf.UserIntf;
import org.zju.demo.utils.Constant;

/**
 * Created by admin on 2015/8/25.
 */
@RestController
public class UserAPI {
    @Autowired
    private UserIntf userService;

    @RequestMapping(value = "/api/user/register", method = RequestMethod.POST)
    public BasicResponseJson register(@RequestBody UserJson userJson) {
        BasicResponseJson response = new BasicResponseJson();
        response.setCommand_id(Constant.COMMAND_REGISTER);

        if (userJson.getCommand_id() == Constant.COMMAND_REGISTER) {
            int response_code = userService.register(userJson.getUsername(), userJson.getUsername(), userJson.getPassword());

            if (response_code == Constant.SUCCESS) {
                response.setResult(Constant.SUCCESS);
                response.setMessage(Constant.MSG_SUCCESS);
            } else if (response_code == Constant.ALREADY_EXISTS) {
                response.setResult(Constant.ALREADY_EXISTS);
                response.setMessage(Constant.MSG_ALREADY_REGISTER);
            } else {
                response.setResult(Constant.UNKNOWN_REASON);
                response.setMessage(Constant.MSG_UNKNOWN_REASON);
            }
        }
        else {
            response.setResult(Constant.UNKNOWN_REASON);
            response.setMessage(Constant.MSG_UNKNOWN_REASON);
        }
        return response;
    }

    @RequestMapping(value = "/api/user/login", method = RequestMethod.POST)
    public BasicResponseJson login(@RequestBody UserJson userJson) {
        BasicResponseJson responseJson = new BasicResponseJson();
        responseJson.setCommand_id(Constant.COMMAND_LOGIN);
        if (userJson.getCommand_id() == Constant.COMMAND_LOGIN) {
            int error = userService.login(userJson.getUsername(), userJson.getPassword());
            if (error == Constant.SUCCESS) {
                responseJson.setResult(Constant.SUCCESS);
                responseJson.setMessage(Constant.MSG_SUCCESS);
            } else if (error == Constant.NO_SUCH_USER) {
                responseJson.setResult(Constant.NO_SUCH_USER);
                responseJson.setMessage(Constant.MSG_NO_SUCH_USER);
            } else {
                responseJson.setResult(Constant.MISMATCH);
                responseJson.setMessage(Constant.MSG_MISMATCH);
            }
        } else {
            responseJson.setResult(Constant.UNKNOWN_REASON);
            responseJson.setMessage(Constant.MSG_UNKNOWN_REASON);
        }
        return responseJson;
    }

    @RequestMapping(value = "/api/user/getInfo", method = RequestMethod.POST)
    public UserDetailJson getUserInfo(@RequestBody UserJson userJson) {
        User user = userService.findByUsername(userJson.getUsername());
        UserDetailJson userDetailJson = new UserDetailJson();
        userDetailJson.setCommand_id(Constant.COMMAND_QUERYUSER);

        if (userJson.getCommand_id() != Constant.COMMAND_QUERYUSER) {
            userDetailJson.setResult(Constant.UNKNOWN_REASON);
            userDetailJson.setMessage(Constant.MSG_UNKNOWN_REASON);
            return userDetailJson;
        }

        if (user != null) {
            userDetailJson.setLevel(user.getLevel());
            userDetailJson.setResult(Constant.SUCCESS);
            userDetailJson.setMessage(Constant.MSG_SUCCESS);
        } else{
            userDetailJson.setResult(Constant.NO_SUCH_USER);
            userDetailJson.setMessage(Constant.MSG_NO_SUCH_USER);
        }
        return userDetailJson;
    }

    @RequestMapping(value = "/api/user/modify", method = RequestMethod.POST)
    public BasicResponseJson modifyUserInfo(@RequestBody UserJson userJson) {
        BasicResponseJson responseJson = new BasicResponseJson();
        responseJson.setCommand_id(Constant.COMMAND_UPDATEUSER);

        if (userJson.getCommand_id() != Constant.COMMAND_UPDATEUSER) {
            responseJson.setResult(Constant.UNKNOWN_REASON);
            responseJson.setMessage(Constant.MSG_UNKNOWN_REASON);
            return responseJson;
        }

        User user = userService.findByUsername(userJson.getUsername());
        if (user == null) {
            responseJson.setResult(Constant.NO_SUCH_USER);
            responseJson.setMessage(Constant.MSG_NO_SUCH_USER);
            return responseJson;
        }
        if (!user.getPassword().equals(userJson.getOldpassword())) {
            responseJson.setResult(Constant.MISMATCH);
            responseJson.setMessage(Constant.MSG_MISMATCH);
            return responseJson;
        }

        user.setPassword(userJson.getNewpassword());
        user.setNickname(userJson.getNickname());
        userService.update(user);

        responseJson.setResult(Constant.SUCCESS);
        responseJson.setMessage(Constant.MSG_SUCCESS);

        return responseJson;
    }
}
